const app = getApp();
const db = wx.cloud.database();
Page({
    data: {
        deskId: 0,
        cart: [],
        orderTime: '',
        total: 0,
        storeInfo: {},
        location: {}
    },
    onReady(){
        this.popupBox = this.selectComponent("#popupBox");
    },
    onShow() {
        db.collection("store").doc("6d85a2b9629311a7076342b322de9447").get().then(res => {
            this.setData({
                storeInfo: res.data,
                location: res.data.location
            });
        });
        this.setData({
            deskId: app.globalData.deskId,
            cart: app.globalData.cart,
            orderTime: app.globalData.orderTime
        });
        let temp = this.data.cart.reduce((pre, current) => {
            let currentPrice = current.real * current.number;
            return pre += currentPrice;
        }, 0);
        this.setData({
            total: temp
        });
    },
    backOperation() {
        const pages = getCurrentPages();        //获取当前的页面栈
        const prePage = pages[pages.length - 2];//获取当前页的上一页
        if(prePage.route=="pages/index/index"){
            wx.navigateBack({
                delta: 1,
            });
        }else{
            wx.navigateBack({
                delta: 3,
            });
        }
    },
    call(event) {
        wx.makePhoneCall({
            phoneNumber: event.currentTarget.dataset.phone,
            success: function () {
                console.log('success');
            }
        })
    },
    location() {
        app.globalData.location = this.data.location;
        wx.navigateTo({
            url: '../map/map',
        })
    },
    settleAccounts() {
        //弹出整单备注
        this.popupBox.switch();
    },
    async submitOrder(event){
        let content = event.detail.content;
        try {
            let res = await db.collection("orders").add({
                data: {
                    deskId: this.data.deskId,
                    orderDetail: this.data.cart,
                    orderTime: this.data.orderTime,
                    total: this.data.total,
                    content:content
                }
            });
        } catch (e) {
            console.error('insert into data fail!');
        }
        wx.navigateTo({
            url: '../settleAccounts/settleAccounts',
        })
    }
})